package com.mohe.zhihui.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mohe.zhihui.model.Activity;
import com.mohe.zhihui.model.Notice;
import com.mohe.zhihui.service.interfaces.IActivity;
import com.mohe.zhihui.service.interfaces.INotice;
import com.mohe.zhihui.utils.Json;
import com.mohe.zhihui.utils.JsonUtil;
import com.mohe.zhihui.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

public class ActivityAction extends ActionSupport {
	
	private static final long serialVersionUID = 3970501433611792664L;

//	ApplicationContext ac;
	
	private IActivity activityServ;
	
	private List<Activity> activityList;
	private Activity activity = new Activity();
	private Page page = new Page();
	
	private List<File> image; // 上传的图片
	private List<String> imageFileName; // 文件名称
	private String title; // 活动标题
	private String content; // 活动内容
	private Long timeKey; // 活动id
	private String picDesc; // 活动展示图片描述
	
	private String flag; // 消息提示
	
	private File imageFile;

	public IActivity getActivityServ() {
		return activityServ;
	}

	public void setActivityServ(IActivity activityServ) {
		this.activityServ = activityServ;
	}

	public List<Activity> getActivityList() {
		return activityList;
	}

	public void setActivityList(List<Activity> activityList) {
		this.activityList = activityList;
	}

	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	
	public List<File> getImage() {
		return image;
	}

	public void setImage(List<File> image) {
		this.image = image;
	}

	public List<String> getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(List<String> imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Long getTimeKey() {
		return timeKey;
	}

	public void setTimeKey(Long timeKey) {
		this.timeKey = timeKey;
	}

	public String getPicDesc() {
		return picDesc;
	}

	public void setPicDesc(String picDesc) {
		this.picDesc = picDesc;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public File getImageFile() {
		return imageFile;
	}

	public void setImageFile(File imageFile) {
		this.imageFile = imageFile;
	}
	
//	public ActivityAction() {
//		super();
//		ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//		activityServ = (IActivity) ac.getBean("IActivity");
//	}


	public String getActivityListForPage() {
		int count = activityServ.getCount(activity);
		page.setCount(count);
		activityList = activityServ.getActivityList(activity, page);
		return "activityList.jsp";
	}

	public void searchActivityList() {
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = activityServ.getCount(activity);
		page.setCount(count);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Activity> list = activityServ.getActivityList(activity, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (Activity s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getTitle())) {
					map.put("title", s.getTitle());
				} else {
					map.put("title", "");
				}
				if (s.getCreateDate() != null) {
					map.put("date", df.format(s.getCreateDate()));
				} else {
					map.put("date", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}

	/**
	 * 保存活动信息
	 */
	public void saveActivity() {
////		Json json = new Json();
//		Activity a = new Activity();
//		a.setTitle(title);
//		a.setContent(content);
//		a.setPicDesc(picDesc);
//		a.setPic(getImagePath().get(0));
//		boolean result = activityServ.saveActivity(a);
////		if (result) {
////			json.setMessage("添加成功");
////		} else {
////			json.setSuccess(false);
////			json.setMessage("添加失败");
////		}
////		JsonUtil.writeJson(json);
//		
//		if (result) {
//			flag = "saveOK";
//		} else {
//			flag = "saveNO";
//		}
//		
//		return "activity.jsp";
		
		
		Activity a = new Activity();
		a.setTitle(title);
		a.setContent(content);
		a.setPicDesc(picDesc);
		image = new ArrayList<File>();
		image.add(imageFile);
		String name = "tmp.jpg";
		imageFileName = new ArrayList<String>();
		imageFileName.add(name);
		a.setPic(getImagePath().get(0));
		
		boolean result = activityServ.saveActivity(a);
		Json json = new Json();
		if (result) {
			json.setMessage("添加成功");
		} else {
			json.setSuccess(false);
			json.setMessage("添加失败");
		}
		JsonUtil.writeJson(json);
	}

	public String toEditActivity() {
		activity = activityServ.getActivityById(activity.getTimeKey());
		return "updateActivity.jsp";
	}

	public void updateActivity() {
////		Json json = new Json();
////		Activity tmp = activityServ.getActivityById(activity.getTimeKey());
//		Activity tmp = activityServ.getActivityById(timeKey);
//		if (tmp != null) {
////			tmp.setTitle(activity.getTitle());
////			tmp.setContent(activity.getContent());
//			tmp.setTitle(title);
//			tmp.setContent(content);
//			tmp.setPicDesc(picDesc);
//			List<String> imageList = getImagePath();
//			if(imageList != null && imageList.size() > 0){ // 可能没有上传图片
//				tmp.setPic(imageList.get(0));	
//			}
//			tmp.setCreateDate(new Date());
//			boolean result = activityServ.updateActivity(tmp);
//			if (result) {
////				json.setMessage("编辑成功");
//				flag = "updateOK";
//				activity = tmp;
//			} else {
////				json.setSuccess(true);
////				json.setMessage("编辑失败");
//				flag = "updateNO";
//			}
//		} else {
////			json.setSuccess(true);
////			json.setMessage("编辑失败");
//			flag = "updateNO";
//		}
////		JsonUtil.writeJson(json);
//		activity = new Activity();
//		return getActivityListForPage();
		
		
		Json json = new Json();
		Activity tmp = activityServ.getActivityById(timeKey);
		if (tmp != null) {
			tmp.setTitle(title);
			tmp.setContent(content);
			tmp.setPicDesc(picDesc);
			if(imageFile != null){ // 可能没有上传图片
				image = new ArrayList<File>();
				image.add(imageFile);
				String name = "tmp.jpg";
				imageFileName = new ArrayList<String>();
				imageFileName.add(name);
				tmp.setPic(getImagePath().get(0));
			}
			tmp.setCreateDate(new Date());
			boolean result = activityServ.updateActivity(tmp);
			if (result) {
				json.setMessage("编辑成功");
				activity = tmp;
			} else {
				json.setSuccess(true);
				json.setMessage("编辑失败");
			}
		} else {
			json.setSuccess(true);
			json.setMessage("编辑失败");
		}
		JsonUtil.writeJson(json);
		
	}

	public void delActivity() {
		Json json = new Json();
		boolean result = activityServ.delActivity(activity);
		if (result) {
			json.setMessage("删除成功");
		} else {
			json.setSuccess(false);
			json.setSuccess(false);
		}
		JsonUtil.writeJson(json);
	}
	
	private List<String> getImagePath(){
		String fileDir = ServletActionContext.getServletContext().getRealPath("pics");
		File filePath = new File(fileDir);
		if(!filePath.exists()){
			filePath.mkdir();
		}
		List<String> slist = new ArrayList<String>();
		List<File> flist = getImage();
		if(flist!=null && flist.size()>0){
			for(int i=0;i<flist.size();i++){
				String geshi = imageFileName.get(i).substring(imageFileName.get(i).lastIndexOf("."));
				FileInputStream fi =null;
				FileOutputStream fo =null;
				String fileName="";
				try {
					fi = new FileInputStream(flist.get(i));
					File newFile = new File(filePath,System.nanoTime()+geshi);
					fo = new FileOutputStream(newFile);
					int length=0;
					byte[] bytes = new byte[2048];
					while((length=fi.read(bytes))>0){
						fo.write(bytes,0,length);
					}
					fileName =newFile.getName();
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					if(fi!=null){
						try {
							fi.close();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					if(fo!=null){
						try {
							fo.close();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
				slist.add(fileName);
			}
		}
		return slist;
	}

}